package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.PickSelectEntity2;
import com.sgb.kjwl.model.entity.TwoLevelEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.view.ui.adapter.enterprise.ThreeLevelSelectAdapter;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;

import java.util.ArrayList;
import java.util.List;

public class ThreeLevelPopWindow<D extends PickSelectEntity2> extends BasePopWindow<AddressEntity<List<AddressEntity<List<D>>>>> {
    private ThreeLevelSelectAdapter<AddressEntity<List<AddressEntity<List<D>>>>> mProvinceAdapter;
    private ThreeLevelSelectAdapter<AddressEntity<List<D>>> mCityAdapter;
    private ThreeLevelSelectAdapter<D> mAreaAdapter;
    private MaxHeightRecyclerView mProvinceRecyclerView,mCityRecyclerView,mAreaRecyclerView;
    private List<AddressEntity<List<AddressEntity<List<D>>>>> mProvinceEntities;
    private AddressEntity<List<AddressEntity<List<D>>>> mProvinceEntity;
    private List<AddressEntity<List<D>>> mCityEntities = new ArrayList<>();
    private AddressEntity<List<D>> mCityEntity;
    private List<D> mAreaEntities = new ArrayList<>();
    private String mProvinceName,mCityName;
    private List<D> mAreEntity = new ArrayList<>();
    private String mProvinceDefaultText,mCityDefaultText,mAreaDefaultText;

    public ThreeLevelPopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_for_manage_three_level;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mProvinceRecyclerView = holder.getPop().findViewById(R.id.mhrv_three_level_1);
        mCityRecyclerView = holder.getPop().findViewById(R.id.mhrv_three_level_2);
        mAreaRecyclerView = holder.getPop().findViewById(R.id.mhrv_three_level_3);
        TextView mTvOkView = holder.getPop().findViewById(R.id.tv_ok);
        TextView mTvNext = holder.getPop().findViewById(R.id.tv_next);

        mTvOkView.setOnClickListener(v -> {
            dissMiss();
            if (onResultClick != null) {
                onResultClick.result(mProvinceEntity, mCityEntity, mAreEntity);
            }
        });

        mTvNext.setOnClickListener(v -> {
            mCityRecyclerView.setVisibility(View.INVISIBLE);
            mAreaRecyclerView.setVisibility(View.INVISIBLE);
            setDefItem(0);
        });

        mProvinceAdapter = new ThreeLevelSelectAdapter<>(mContext, null);
        mProvinceRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mProvinceRecyclerView.setAdapter(mProvinceAdapter);
        mProvinceAdapter.setItemSelect(true);
        mProvinceAdapter.setDefItem(0);
        mProvinceAdapter.setOnItemClick(new AdapterOnItemClick<AddressEntity<List<AddressEntity<List<D>>>>>() {
            @Override
            public void onItemClick(AddressEntity<List<AddressEntity<List<D>>>> listTwoLevelEntity, int position) {
                mProvinceEntity = listTwoLevelEntity;
                if (!listTwoLevelEntity.getName().equals(mProvinceDefaultText)){
                    mCityRecyclerView.setVisibility(View.VISIBLE);
                    mAreaRecyclerView.setVisibility(View.VISIBLE);
                    if (mProvinceName != null && !mProvinceName.equals(listTwoLevelEntity.getName())) {
                        mCityAdapter.setDefItem(1);
                        mCityAdapter.resetCheckDatas();
                    }
                    mProvinceName = listTwoLevelEntity.getName();
//                    lineView.setVisibility(View.VISIBLE);
//                    bottomLayout.setVisibility(View.VISIBLE);

                    mCityEntities = listTwoLevelEntity.getChildren();
                    if (mCityEntities.size() == 0 || !mCityEntities.get(0).getName().equals(mCityDefaultText)) {
                        AddressEntity<List<D>> entity = new AddressEntity<>();
                        entity.setNo("");
                        entity.setName(mCityDefaultText);
                        mCityEntities.add(0, entity);
                    }
                    mCityAdapter.setDatas(mCityEntities);
                    if (mCityEntities.size() > 0) {
                        mCityEntity = mCityEntities.get(0);
                    }

                    mAreaEntities = listTwoLevelEntity.getChildren().get(1).getChildren();
                    if (mAreaEntities.size() == 0 || !mAreaEntities.get(0).getName().equals(mAreaDefaultText)) {
                        D entity = (D) new PickSelectEntity2();
                        entity.setNo("");
                        entity.setName(mAreaDefaultText);
                        mAreaEntities.add(0, entity);
                    }
                    mAreaAdapter.setDatas(mAreaEntities);
                    if (mAreaEntities.size() > 0) {
                        mAreEntity.clear();
                        mAreEntity.add(mAreaEntities.get(0));
                    }
                }else {
                    mCityRecyclerView.setVisibility(View.INVISIBLE);
                    mAreaRecyclerView.setVisibility(View.INVISIBLE);
                    AddressEntity<List<AddressEntity<List<D>>>> province = new AddressEntity<>();
                    province.setName(mProvinceDefaultText);
                    province.setNo("");
                    province.setId("");
                    mProvinceEntity = province;
                }
            }
        });

        mCityAdapter = new ThreeLevelSelectAdapter<>(mContext, null);
        mCityAdapter.setItemSelect(true);
        mCityAdapter.setDefItem(0);
        mCityRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mCityRecyclerView.setAdapter(mCityAdapter);
        mCityAdapter.setOnItemClick(new AdapterOnItemClick<AddressEntity<List<D>>>() {
            @Override
            public void onItemClick(AddressEntity<List<D>> listTwoLevelEntity, int position) {
                mCityEntity = listTwoLevelEntity;

                if (!listTwoLevelEntity.getName().equals(mProvinceDefaultText)){
                    if (mCityName != null && !mCityName.equals(listTwoLevelEntity.getName())){
                        mAreaAdapter.setDefItem(1);
                        mAreaAdapter.resetCheckDatas();
                    }
                    mCityName = listTwoLevelEntity.getName();

                    mAreaEntities = listTwoLevelEntity.getChildren();
                    if (mAreaEntities.size() == 0) {
                        D entity = (D) new PickSelectEntity2();
                        entity.setNo("");
                        entity.setName(mAreaDefaultText);
                        mAreaEntities.add(0, entity);
                    } else {
                        if (!mAreaEntities.get(0).getName().equals(mAreaDefaultText)){
                            D entity = (D) new PickSelectEntity2();
                            entity.setNo("");
                            entity.setName(mAreaDefaultText);
                            mAreaEntities.add(0, entity);
                        }
                    }

                    mAreaAdapter.setDatas(mAreaEntities);
                    if (mAreaEntities.size() > 0) {
                        mAreEntity.clear();
                        mAreEntity.add(mAreaEntities.get(0));
                    }
                }else {
                    mAreaRecyclerView.setVisibility(View.INVISIBLE);
                    AddressEntity<List<D>> city = new AddressEntity<>();
                    city.setName(mProvinceDefaultText);
                    city.setNo("");
                    city.setId("");
                    mCityEntity = city;
                }
            }
        });

        mAreaAdapter = new ThreeLevelSelectAdapter<>(mContext, null);
        mAreaAdapter.setMultiSelect(true);
        mAreaAdapter.setDefItem(0);
        mAreaRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAreaRecyclerView.setAdapter(mAreaAdapter);
        mAreaAdapter.setOnItemMultiClick((ds, position) -> mAreEntity = ds);

        holder.getPop().findViewById(R.id.bg_pop_gray_layout).setOnClickListener(view -> dissMiss());
    }

    @Override
    protected void setData(List<AddressEntity<List<AddressEntity<List<D>>>>> data) {
        this.mProvinceEntities = data;
        mProvinceAdapter.setDatas(data);
    }


    @Override
    protected List<AddressEntity<List<AddressEntity<List<D>>>>> getData() {
        return mProvinceAdapter.getDatas();
    }


    public void setDefItem(int...position) {
        for (int i = 0; i < position.length; i++) {
            if (i == 0){
                mProvinceAdapter.setDefItem(position[i]);
            }

            if (i == 1){
                mProvinceAdapter.setDefItem(position[i]);
            }

            if (i == 2){
                mProvinceAdapter.setDefItem(position[i]);
            }
        }
    }

    public void setBackground(@ColorInt int...color){
        for (int i = 0; i < color.length; i++) {
            if (i == 0){
                mProvinceRecyclerView.setBackgroundColor(color[i]);
            }

            if (i == 1){
                mCityRecyclerView.setBackgroundColor(color[i]);
            }

            if (i == 2){
                mAreaRecyclerView.setBackgroundColor(color[i]);
            }
        }
    }

    public void setDefault(String...defaultItem){
        int province = 0,city = 0,area = 0;
        if (defaultItem.length == 0){
            return;
        }

        if (ThreeLevelSelectAdapter.sIsSimple){
            for (int i = 0; i < mProvinceEntities.size(); i++) {
                if (mProvinceEntities.get(i).getSimpleName().equals(defaultItem[0])){
                    province = i;
                    break;
                }
            }

            if (defaultItem.length == 1){
                return;
            }

            for (int i = 0; i < mCityEntities.size(); i++) {
                if (mCityEntities.get(i).getSimpleName().equals(defaultItem[1])){
                    city = i;
                    break;
                }
            }

            if (defaultItem.length == 2){
                return;
            }

            for (int i = 0; i < mAreaEntities.size(); i++) {
                if (mAreaEntities.get(i).getSimpleName().equals(defaultItem[2])){
                    area = i;
                    break;
                }
            }
        }else {
            for (int i = 0; i < mProvinceEntities.size(); i++) {
                if (mProvinceEntities.get(i).getName().equals(defaultItem[0])){
                    province = i;
                    break;
                }
            }

            if (defaultItem.length == 1){
                return;
            }

            for (int i = 0; i < mCityEntities.size(); i++) {
                if (mCityEntities.get(i).getName().equals(defaultItem[1])){
                    city = i;
                    break;
                }
            }

            if (defaultItem.length == 2){
                return;
            }

            for (int i = 0; i < mAreaEntities.size(); i++) {
                if (mAreaEntities.get(i).getName().equals(defaultItem[2])){
                    area = i;
                    break;
                }
            }
        }

        setDefItem(province,city,area);
    }

    public void isSimple(boolean isSimple){
        ThreeLevelSelectAdapter.sIsSimple = isSimple;
    }

    public void setDefaultText(String...defaultText){
        if (defaultText.length == 0){
            mProvinceDefaultText = "全部";
            mCityDefaultText = "全部";
            mAreaDefaultText = "全部";
            return;
        }

        if (defaultText[0].equals("")&&defaultText[1].equals("")&&defaultText[2].equals("")){
            mProvinceDefaultText = "全部";
            mCityDefaultText = "全部";
            mAreaDefaultText = "全部";
            return;
        }

        if (defaultText.length == 1){
            mProvinceDefaultText = defaultText[0];
            mCityDefaultText = "全部";
            mAreaDefaultText = "全部";
            return;
        }

        if (defaultText.length == 2){
            mProvinceDefaultText = defaultText[0];
            mCityDefaultText = defaultText[1];
            mAreaDefaultText = "全部";
            return;
        }

        mProvinceDefaultText = defaultText[0];
        mCityDefaultText = defaultText[1];
        mAreaDefaultText = defaultText[2];
    }


}
